package com.tadpole.music.presenter.home;

import com.tadpole.music.bean.home.ClockInRecordBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.home.ClockInRecordIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取打卡记录
 * Created By 孙岩
 * on 2020/12/10
 */
public class ClockInRecordPresenter extends BasePresenter<ClockInRecordIView> {
    @SuppressWarnings("unchecked")
    public void getRecord(){
        if(isViewAttached()){
            String url = UrlConfig.getClockInRecord;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN,"");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    JSONObject logsObj = new JSONObject(object.getString("logs"));
                                    List<Integer> logsBeanList = new ArrayList<>();
                                    JSONArray logsArray = new JSONArray(logsObj.getString("logs"));
                                    for(int i=0;i<logsArray.length();i++){
                                        logsBeanList.add(logsArray.getInt(i));
                                    }
                                    ClockInRecordBean.DataBean.LogsBean logsBean = new ClockInRecordBean.DataBean.LogsBean(
                                            JsonParseUtil.getInt(logsObj,"month"),
                                            logsBeanList
                                    );

                                    JSONObject signObj = new JSONObject(object.getString("sign"));
                                    ClockInRecordBean.DataBean.SignBean signBean = new ClockInRecordBean.DataBean.SignBean(
                                            JsonParseUtil.getInt(signObj,"id"),
                                            JsonParseUtil.getStr(signObj,"created_at"),
                                            JsonParseUtil.getStr(signObj,"updated_at"),
                                            JsonParseUtil.getInt(signObj,"user_id"),
                                            JsonParseUtil.getInt(signObj,"continue_sign_days"),
                                            JsonParseUtil.getInt(signObj,"total_sign_days")
                                    );

                                    ClockInRecordBean.DataBean bean = new ClockInRecordBean.DataBean(
                                            logsBean,
                                            signBean,
                                            JsonParseUtil.getInt(object,"points"),
                                            JsonParseUtil.getInt(object,"sign_points"),
                                            JsonParseUtil.getBoolean(object,"signed")
                                    );
                                    getView().showRecord(bean);
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
